// JavaScript Document

var codeProcess = [];
var codeTime = [];
var nowCheer = 0;

//以下代码在页面加载完成后执行
$(function() {
	LoadPage();

	$("#detailBox img").attr("onMouseMove","this.style.opacity=1");
	$("#detailBox img").attr("onMouseOut","this.style.opacity=0.4");
	$("#detailBox img").click(function () {
		$("#detailBox").hide();
		document.documentElement.style.overflow = '';
	});
	$("*").css("overflowX","hidden");
	$("#mainBox-value>div>button").click(function(){
		var $thisDom = $("#mainBox-value>div").children("#"+$(this).attr("name"));
		if($thisDom.is(":hidden")){
			$(this).css("marginBottom","0px");
			$thisDom.slideDown(500);
		}else{
			$thisDom.slideUp(200,function(){
				var $soon = $("#mainBox-value>div>div:hidden");
				for(var i = 0; i < $soon.length; i++){
					$("#mainBox-value>div>button[name='"+$($soon[i]).attr("id")+"']").css("marginBottom","10px");
				}
			});
		}
	});

	$("#mainBox-value button").click(function () {
		var matchMessage = JSON.parse($(this).parent().children("p:last").text());

		//以下内容加载数据
		document.getElementById("loadData").innerHTML = "<tr class=\"table-title\"><td>红方</td><td>蓝方</td><td>执黑棋</td><td>结果</td><td>复盘</td></tr>";
		$("#loadData").append("<tr class='table-value'></tr>");
		var $thisDom = $("#loadData tr:last");
		$thisDom.append("<td style=\"color: #ff2631;\">"+matchMessage["userOne"]+"</td>");
		$thisDom.append("<td style=\"color: #00aeff;\">"+matchMessage["userTwo"]+"</td>");
		$thisDom.append("<td style='color: #00aeff;'>蓝方</td>");
		if(matchMessage["firstWinner"] === "none") {
			$thisDom.append("<td style='color: green;'>平局</td>");
		}else if(matchMessage["firstWinner"] === matchMessage["userOne"]){
			$thisDom.append("<td style='color: red;'>红方</td>");
		}else {
			$thisDom.append("<td style='color: blue;'>蓝方</td>");
		}
		$thisDom.append("<td><a style='cursor: pointer;color: blue;'>点击复盘</a></td>");
		$thisDom.children("td:last").append("<p hidden='hidden'>"+matchMessage["firstCodeResult"]+"$$"+matchMessage["firstTime"]+"</p>");

		$("#loadData").append("<tr class='table-value'></tr>");
		$thisDom = $("#loadData tr:last");
		$thisDom.append("<td style=\"color: #ff2631;\">"+matchMessage["userOne"]+"</td>");
		$thisDom.append("<td style=\"color: #00aeff;\">"+matchMessage["userTwo"]+"</td>");
		$thisDom.append("<td style='color: red;'>红方</td>");
		if(matchMessage["secondWinner"] === "none") {
			$thisDom.append("<td style='color: green;'>平局</td>");
		}else if(matchMessage["secondWinner"] === matchMessage["userOne"]){
			$thisDom.append("<td style='color: red;'>红方</td>");
		}else {
			$thisDom.append("<td style='color: blue;'>蓝方</td>");
		}
		$thisDom.append("<td><a style='cursor: pointer;color: blue;'>点击复盘</a></td>");
		$thisDom.children("td:last").append("<p hidden='hidden'>"+matchMessage["secondCodeResult"]+"$$"+matchMessage["secondTime"]+"</p>");
		//以上内容加载数据

		document.documentElement.style.overflow = 'hidden';
		$("#detailBox").show();
		$("#detailBox").css("top",window.pageYOffset);

		$("#detailBox a").click(function () {
			var result = $(this).parent().children("p").text();
			var time = result.substring(result.indexOf("$$")+2);
			result = result.substring(0,result.indexOf("$$"));
			codeProcess = [];
			codeTime = [];
			nowCheer = 0;
			$("#cheerReplay>div *:gt(9)").remove();
			while (result.length > 0){
				codeProcess.push(result.substring(0,result.indexOf("#")));
				result = result.substring(result.indexOf("#")+1);
			}
			while (time.length > 0){
				codeTime.push(time.substring(0,time.indexOf("#")));
				time = time.substring(time.indexOf("#")+1);
			}
			rightStep();
			$("#detailBox").hide();
			$("#cheerReplay").show();
		});
	});
});

function LoadPage(){
	var pageValue = match["codeResult"];
	var forJudge = true;
	for(var i = 1; forJudge; i++){
		var titleOne = "第" + i + "轮";
		var titleTwo = "第" + (i+1) + "轮";
		if(pageValue.search(titleTwo) !== -1){
			var messageOne = pageValue.substring(pageValue.search(titleOne)+3,pageValue.search(titleTwo));
			pageValue = pageValue.substring(pageValue.search(titleTwo));
			$("#mainBox-value>div").append("<button class=\"btn btn-info dropdown-toggle mainBoxValue-btn\">"+titleOne+"&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"caret\"></span></button>");
			$("#mainBox-value>div button:last").attr("name","box"+i);
			$("#mainBox-value>div").append("<div id='box"+i+"' class=\"mainBoxValue-vsBox well\" hidden=\"hidden\"></div>");
			var $thisDom = $("#mainBox-value>div>div:last");
			var forJudgeTwo = true;
			for(var j = 1; forJudgeTwo; j++){
				$thisDom.append("<div class=\"mainBoxValueVSBox-OneRow\"></div>");
				$thisDom = $thisDom.children("div:last");
				titleOne = "第" + j + "场";
				titleTwo = "第" + (j+1) + "场";
				var matchMessage;
				if(messageOne.indexOf(titleTwo) !== -1) {
					matchMessage = messageOne.substring(messageOne.indexOf(titleOne)+3,messageOne.indexOf(titleTwo));
				}else{
					matchMessage = messageOne.substring(messageOne.indexOf(titleOne)+3);
					forJudgeTwo = false;
				}
				//以下代码处理数据
				var userOne = matchMessage.substring(matchMessage.indexOf(titleOne), matchMessage.search("VS"));
				var userTwo = matchMessage.substring(matchMessage.search("VS") + 2, matchMessage.search("winner"));
				var firstMatchWinner = matchMessage.substring(matchMessage.search("winner") + 6, matchMessage.indexOf("$"));
				matchMessage = matchMessage.substring(matchMessage.indexOf("$") + 1);
				var firstMatchCodeResult = matchMessage.substring(0, matchMessage.indexOf("$"));
				matchMessage = matchMessage.substring(matchMessage.indexOf("$") + 1);
				var firstMatchTime = matchMessage.substring(0, matchMessage.indexOf("$"));
				matchMessage = matchMessage.substring(matchMessage.indexOf("$") + 1);
				var secondMatchWinner = matchMessage.substring(matchMessage.indexOf("winner") + 6, matchMessage.indexOf("$"));
				matchMessage = matchMessage.substring(matchMessage.indexOf("$") + 1);
				var secondMatchCodeResult = matchMessage.substring(0, matchMessage.indexOf("$"));
				matchMessage = matchMessage.substring(matchMessage.indexOf("$") + 1);
				var secondMatchTime = matchMessage.substring(0, matchMessage.indexOf("$"));
				matchMessage = matchMessage.substring(matchMessage.indexOf("$") + 1);
				var finalWinner = matchMessage.substring(matchMessage.indexOf("finalWinner")+11);
				//以上代码处理数据

				$thisDom.append("<img src=\"icon/winner.png\" class=\"mainBoxValueVSBox-winImg\">");
				$thisDom.append("<p class=\"mainBoxValueVSBox-user\"></p>");
				$thisDom.append("<img src=\"icon/vs.png\" class=\"mainBoxValueVSBox-vsImg\">");
				$thisDom.append("<p class=\"mainBoxValueVSBox-user\"></p>");
				$thisDom.append("<button class=\"btn btn-success mainBoxValueVSBox-btn\">查看详情</button>");
				if(finalWinner === userOne){
					$thisDom.children("p:first").text(userOne);
					$thisDom.children("p:last").text(userTwo);
				}else{
					$thisDom.children("p:first").text(userTwo);
					$thisDom.children("p:last").text(userOne);
				}
				$thisDom.append("<div class=\"row-line\"></div><p hidden=\"hidden\"></p>");
				var soonStr = {};
				soonStr["firstWinner"] = firstMatchWinner;
				soonStr["firstCodeResult"] = firstMatchCodeResult;
				soonStr["firstTime"] = firstMatchTime;
				soonStr["secondWinner"] = secondMatchWinner;
				soonStr["secondCodeResult"] = secondMatchCodeResult;
				soonStr["secondTime"] = secondMatchTime;
				soonStr["userOne"] = userOne;
				soonStr["userTwo"] = userTwo;
				$thisDom.children("p:last").text(JSON.stringify(soonStr));

			}
		}else{
			forJudge = false;
			titleTwo = pageValue.substring(pageValue.indexOf("$$thisMatchWinner$$")+19);
			$("#mainBox-head p:first").text(titleTwo);
		}
	}
}

function leftStep() {
	if(nowCheer < 2)
		alert("已经是第一步了！");
	else {
		document.getElementById("runTime").innerText = codeTime[nowCheer-2];
		var position = Number(codeProcess[nowCheer - 1]);
		$("#cheer" + position).remove();
		nowCheer -= 1;
	}
}

function rightStep() {
	if(nowCheer+1 > codeProcess.length)
		alert("已经是最后一步了");
	else {
		var x = Number(codeProcess[nowCheer]), y;
		y = x % 100;
		x = Math.round(x / 100);
		if (nowCheer % 2 === 0) {
			AddBlackCheer(x, y);
		} else {
			AddWhiteCheer(x, y);
		}
		document.getElementById("runTime").innerText = codeTime[nowCheer];
		nowCheer += 1;
	}
}


var blackleft=53,blacktop=110;//0代表下一步下黑棋，1代表下一步下白棋
function AddBlackCheer(x,y) {
	var position = x*100 + y;
	var a=blackleft+26*y;
	var b=blacktop+26*x;
	$("#cheerReplay>div").append("<img id='cheer"+position+"' src='backstage/img/blackCheer.png' width='28' height='27' style='position: absolute;left: "+a+"px;top:"+b+"px' >");
}
var whiteleft=47,whitetop=107;
function AddWhiteCheer(x,y) {
	var position = x*100 + y;
	var a=whiteleft+26*y;
	var b=whitetop+26*x;
	$("#cheerReplay>div").append("<img id='cheer"+position+"'src='backstage/img/whiteCheer.png' width='38' height='36' style='position: absolute;left: "+a+"px;top:"+b+"px' >");
}

function closeBackCheer(){
	$("#cheerReplay").hide();
	$("#detailBox").show();
}